package com.ysynzehao.db;

import com.ysynzehao.dao.utils.BaseRepositoryFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * niuzehao
 * 2018/12/5 11:58.
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.ysynzehao.dao.user"},
        repositoryFactoryBeanClass = BaseRepositoryFactoryBean.class,
        entityManagerFactoryRef = "userEntityManagerFactory",
        transactionManagerRef = "userTransactionManager")
public class UserDbConfiguration {

    @Autowired
    private Environment environment;

    @Bean(name = "userDataSerouce")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.user")
    public DataSource getUserDataSerouce() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "userEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean getEntiryManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("userDataSerouce") DataSource dataSource, HibernateProperties hibernateProperties) {
        HashMap<String, String> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto",
                environment.getProperty("spring.jpa.hibernate.ddl-auto"));
        // 得到生成表策略 userId -> user_id
        Map<String, Object> properties2 = hibernateProperties.determineHibernateProperties(properties, new HibernateSettings());
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = builder.dataSource(dataSource)
                .properties(properties2)
                .packages("com.ysynzehao.bean.user")
                .build();
        return entityManagerFactoryBean;
    }

    @Primary
    @Bean(name = "userEntityManager")
    public EntityManager getEntityManager(
            @Qualifier("userEntityManagerFactory") LocalContainerEntityManagerFactoryBean factoryBean) {
        return factoryBean.getObject().createEntityManager();
    }

    @Primary
    @Bean(name = "userTransactionManager")
    public PlatformTransactionManager transactionManagerPrimary(@Qualifier("userEntityManager") EntityManager entityManager) {
        return new JpaTransactionManager(entityManager.getEntityManagerFactory());
    }

}
